//const {AsyncParallelHook} = require('tapable');
//异步 说明回调函数里可以写异步代码 并行说明多个回调之间是并行执行的
class AsyncParallelHook{
    constructor(args){
        this._args = args;//['name','age']
        this.taps = [];
    }
    tapAsync(name,fn){
        this.taps.push(fn);
    }
    callAsync(){
         let args = Array.from(arguments).slice(0,this._args.length);
         let finalCallback = arguments[arguments.length-1];
         let i=0,length=this.taps.length;
         function done(){
             if(++i === length){
               finalCallback(null);
             }
         }
         this.taps.forEach(fn=>fn(...args,done));
    }
}
let hook = new AsyncParallelHook(['name','age']);
hook.tapAsync('1',(name,age,done)=>{
    setTimeout(() => {
        console.log('1',name,age);
        done();
    }, 1000);
});
hook.tapAsync('2',(name,age,done)=>{
    setTimeout(() => {
        console.log('2',name,age);
        done();
    }, 2000);
});
hook.tapAsync('3',(name,age,done)=>{
    setTimeout(() => {
        console.log('3',name,age);
        done();
    }, 3000);
});
console.time('cost');
hook.callAsync('zhufeng',10,(err=>{
    console.log(err);
    console.timeEnd('cost');
}));